package com.lei.applt;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicScrollBarUI;

import com.lei.emun.FilePathEnum;
import com.lei.util.FrameUtil;

/**
 * 自定义滚动条UI
 * @author LEI
 * @date 2017年8月10日修改
 */
public class VerticalScrollBarUI extends BasicScrollBarUI {

		@Override
		protected void configureScrollBarColors() {
			// 把手
//			thumbColor = Color.GRAY;
//			thumbHighlightColor = Color.BLUE;
//			thumbDarkShadowColor = Color.BLACK;
//			thumbLightShadowColor = Color.YELLOW;
			
			// 滑道
			trackColor = Color.WHITE;
//			trackHighlightColor = Color.GREEN;
		}

	@Override
	protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
		super.paintTrack(g, c, trackBounds);
	}
	
	@Override
	protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
		// 把绘制区的x，y点坐标定义为坐标系的原点
		// 这句一定一定要加上啊，不然拖动就失效了
		g.translate(thumbBounds.x, thumbBounds.y); 
		// 消除锯齿
		Graphics2D g2 = (Graphics2D) g;
		// 设置把手颜色
		g.setColor(new Color(204,204,204));

		g.drawRect(0, 0,thumbBounds.width, thumbBounds.height-1);
		g2.fillRect(0, 0,thumbBounds.width, thumbBounds.height);
		
		

		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.addRenderingHints(rh);
		// 半透明
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));

		
	}
	
	@Override
	protected void setThumbBounds(int x, int y, int width, int height) {
	super.setThumbBounds(x, y,width, height);
	}
	
	@Override
	protected JButton createIncreaseButton(int orientation) {
		JButton button = new JButton(produceImage("scroll_down.png"));
		button.setBorder(null);
		return button;
	}
	
	@Override
	protected JButton createDecreaseButton(int orientation) {
		JButton button = new JButton(produceImage("scroll_up.png"));
		button.setBorder(null);
		return button;
	}
	
	/**
	 * 获取图片
	 * @param name 图片名称
	 * @return
	 */
	private ImageIcon produceImage(String name) {
		ImageIcon backImage = FrameUtil.getImage(FilePathEnum.UI, name);
		return backImage;
	}
	
}
